package com.fjwt.gz.mch.ctrl.qrcode;

import cn.hutool.json.JSONUtil;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.QrcodeRecordDTO;
import com.fjwt.gz.db.vo.QrcodeRecordVO;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.QrcodeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 生成卡券二维码记录
 */
@Slf4j
@RestController
@RequestMapping("/api/qrcodeRecord")
public class QrcodeRecordController extends CommonCtrl {

    @Autowired
    private QrcodeRecordService qrcodeRecordService;

    /**
     * 新增二维码记录
     */
    @PostMapping("/saveRecord")
    public ApiRes saveRecord(){
        QrcodeRecordDTO dto = getObject(QrcodeRecordDTO.class);
        Long sysUserId = getCurrentUser().getSysUserId();
        log.info("新增卡券二维码记录接口调用，请求地址：/api/qrcodeRecord/saveRecord，请求参数：{}", JSONUtil.toJsonStr(dto));
        qrcodeRecordService.insert(dto,  sysUserId);
        return ApiRes.ok();
    }

    /**
     * 生成二维码、或者查看二维码
     * @return
     */
    @PostMapping("/getUnlimitedQRCode")
    public ApiRes getUnlimitedQRCode(){
        Long qrcodeRecordId = getValLong("qrcodeRecordId");
        log.info("生成查看二维码接口调用，请求地址：/api/qrcodeRecord/getUnlimitedQRCode，请求参数：二维码记录id：{}",qrcodeRecordId);
        String code = qrcodeRecordService.getUnlimitedQRCode(qrcodeRecordId);
        log.info("生成二维码接口调用结果，请求地址：/api/qrcodeRecord/getUnlimitedQRCode，返回结果：{}",code);
        return ApiRes.ok(code);
    }

    /**
     * 获取二维码详情
     * @return
     */
    @PostMapping("/getRecordDetail")
    public ApiRes getRecordDetail(){
        Long qrcodeRecordId = getValLong("qrcodeRecordId");
        log.info("获取二维码详情接口调用，请求路径：/api/qrcodeRecord/getRecordDetail，请求参数：二维码记录id：{}",qrcodeRecordId);
        QrcodeRecordVO vo = qrcodeRecordService.getRecordDetail(qrcodeRecordId);
        log.info("获取二维码详情接口调用结果，请求路径：/api/qrcodeRecord/getRecordDetail，返回结果：{}",JSONUtil.toJsonStr(vo));
        QrcodeRecordVO data = new QrcodeRecordVO();
        BeanUtils.copyProperties(vo, data);
        return ApiRes.ok(data);
    }

    /**
     * 生成拉新邀请小程序二维码、或者查看二维码
     * @return
     */
    @PostMapping("/getInviterUnlimitedQRCode")
    public ApiRes getInviterUnlimitedQRCode(){
        String appId = getCurrentUser().getAppId();
        Long fansId = getCurrentUser().getFansId();
        String pathUrl = getValString("pathUrl");
        log.info("生成拉新邀请小程序二维码接口调用，请求地址：/api/qrcodeRecord/getInviterUnlimitedQRCode，请求参数：appId：{}，fansId：{}，pathUrl：{}",appId,fansId,pathUrl);
        String code = qrcodeRecordService.getInviterUnlimitedQRCode(appId,fansId,pathUrl);
        log.info("生成拉新邀请小程序二维码接口调用结果，请求地址：/api/qrcodeRecord/getInviterUnlimitedQRCode，返回结果：{}",code);
        return ApiRes.ok(code);
    }

}
